package com.topdt.jbpm.form;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import com.thx.common.spring.BeanFactory;
import com.thx.common.util.ThreadLocalContext;
import com.thx.common.web.SessionInfo;
import com.thx.common.web.WebConstant;
import com.topdt.jbpm.Constants;
import com.topdt.jbpm.form.query.Option;
import com.topdt.jbpm.identity.IdFactory;
import com.topdt.jbpm.identity.IdentityService;
import com.topdt.jbpm.identity.User;

/**
 *
 * 建立日期 : 2013-6-4 下午04:07:35<br>
 * 作者 : shiyan<br>
 * 模块 : <br>
 * 描述 : 取系统变量的值<br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 <br>
 * 2 <br>
 */
public class SystemVarEval {
  public static Object getValue(String id) {
    SysParamService sysParamService = (SysParamService) BeanFactory.getBean(SysParamService.class);
    Option option = sysParamService.getOptionName(id, "system");
    if ("currentUser".equals(option.getValue())) {
      HttpServletRequest request =
          (HttpServletRequest) ThreadLocalContext.get(Constants.CONTEXT_HTTPREQ);
      IdentityService identityService = IdFactory.getIdentityServiceFactory().getIdentityService();
      SessionInfo sessionInfo =
          (SessionInfo) request.getSession().getAttribute(WebConstant.KEY_USER);
      User user = identityService.getUserById(sessionInfo.getUserId());
      return user.getName();
    } else if ("currentTime".equals(option.getValue())) {
      Timestamp time = new Timestamp(System.currentTimeMillis());
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      return sdf.format(time);
    }
    // --TODO
    return null;
  }
}
